home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 051-075 / disk_061 / microemacs / bind.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  15KB  |  674 lines

  1. /*    This file is for functions having to do with key bindings,
  2.     descriptions, help commands and startup file.
  3.  
  4.     written 11-feb-86 by Daniel Lawrence
  5.                                 */
  6.  
  7. #include    <stdio.h>
  8. #include    "estruct.h"
  9. #include    "edef.h"
  10. #include    "epath.h"
  11.  
  12. #if    MEGAMAX & ST520
  13. overlay    "bind"
  14. #endif
  15.  
  16. extern int meta(), cex(), unarg(), ctrlg(); /* dummy prefix binding functions */
  17.  
  18. deskey(f, n)    /* describe the command for a certain key */
  19.  
  20. {
  21.     register int c;        /* command character to describe */
  22.     register char *ptr;    /* string pointer to scan output strings */
  23.     register KEYTAB *ktp;    /* pointer into the command table */
  24.     register int found;    /* matched command flag */
  25.     register NBIND *nptr;    /* pointer into the name binding table */
  26.     char outseq[80];    /* output buffer for command sequence */
  27.  
  28.     /* prompt the user to type us a key to describe */
  29.     mlwrite(": describe-key ");
  30.  
  31.     /* get the command sequence to describe */
  32.     c = getckey(FALSE);            /* get a command sequence */
  33.  
  34.     /* change it to something we can print as well */
  35.     cmdstr(c, &outseq[0]);
  36.  
  37.     /* and dump it out */
  38.     ptr = &outseq[0];
  39.     while (*ptr)
  40.         TTputc(*ptr++);
  41.     TTputc(' ');        /* space it out */
  42.  
  43.     /* find the right ->function */
  44.     ktp = &keytab[0];
  45.     found = FALSE;
  46.     while (ktp->k_fp != NULL) {
  47.         if (ktp->k_code == c) {
  48.             found = TRUE;
  49.             break;
  50.         }
  51.         ++ktp;
  52.     }
  53.  
  54.     if (!found)
  55.         strcpy(outseq,"Not Bound");
  56.     else {
  57.         /* match it against the name binding table */
  58.         nptr = &names[0];
  59.         strcpy(outseq,"[Bad binding]");
  60.         while (nptr->n_func != NULL) {
  61.             if (nptr->n_func == ktp->k_fp) {
  62.                 strcpy(outseq, nptr->n_name);
  63.                 break;
  64.             }
  65.             ++nptr;
  66.         }
  67.     }
  68.  
  69.     /* output the command sequence */
  70.     ptr = &outseq[0];
  71.     while (*ptr)
  72.         TTputc(*ptr++);
  73. }
  74.  
  75. cmdstr(c, seq)    /* change a key command to a string we can print out */
  76.  
  77. int c;        /* sequence to translate */
  78. char *seq;    /* destination string for sequence */
  79.  
  80. {
  81.     char *ptr;    /* pointer into current position in sequence */
  82.  
  83.     ptr = seq;
  84.  
  85.     /* apply meta sequence if needed */
  86.     if (c & META) {
  87.         *ptr++ = 'M';
  88.         *ptr++ = '-';
  89.     }
  90.  
  91.     /* apply ^X sequence if needed */
  92.     if (c & CTLX) {
  93.         *ptr++ = '^';
  94.         *ptr++ = 'X';
  95.     }
  96.  
  97.     /* apply SPEC sequence if needed */
  98.     if (c & SPEC) {
  99.         *ptr++ = 'F';
  100.         *ptr++ = 'N';
  101.     }
  102.  
  103.     /* apply control sequence if needed */
  104.     if (c & CTRL) {
  105.         *ptr++ = '^';
  106.     }
  107.  
  108.     c = c & 255;    /* strip the prefixes */
  109.  
  110.     /* and output the final sequence */
  111.  
  112.     *ptr++ = c;
  113.     *ptr = 0;    /* terminate the string */
  114. }
  115.  
  116. help(f, n)    /* give me some help!!!!
  117.            bring up a fake buffer and read the help file
  118.            into it with view mode            */
  119. {
  120.     register WINDOW *wp;    /* scaning pointer to windows */
  121.     register BUFFER *bp;    /* buffer pointer to help */
  122.     char *fname;        /* ptr to file returned by flook() */
  123.  
  124.     /* first check if we are already here */
  125.     bp = bfind("emacs.hlp", FALSE, BFINVS);
  126.  
  127.     if (bp == NULL) {
  128.         fname = flook(pathname[1], FALSE);
  129.         if (fname == NULL) {
  130.             mlwrite("[Help file is not online]");
  131.             return(FALSE);
  132.         }
  133.     }
  134.  
  135.     /* split the current window to make room for the help stuff */
  136.     if (splitwind(FALSE, 1) == FALSE)
  137.             return(FALSE);
  138.  
  139.     if (bp == NULL) {
  140.         /* and read the stuff in */
  141.         if (getfile(fname, FALSE) == FALSE)
  142.             return(FALSE);
  143.     } else
  144.         swbuffer(bp);
  145.  
  146.     /* make this window in VIEW mode, update all mode lines */
  147.     curwp->w_bufp->b_mode |= MDVIEW;
  148.     curwp->w_bufp->b_flag |= BFINVS;
  149.     wp = wheadp;
  150.     while (wp != NULL) {
  151.         wp->w_flag |= WFMODE;
  152.         wp = wp->w_wndp;
  153.     }
  154.     return(TRUE);
  155. }
  156.  
  157. int (*fncmatch(fname))() /* match fname to a function in the names table
  158.                 and return any match or NULL if none        */
  159.  
  160. char *fname;    /* name to attempt to match */
  161.  
  162. {
  163.     register NBIND *ffp;    /* pointer to entry in name binding table */
  164.  
  165.     /* scan through the table, returning any match */
  166.     ffp = &names[0];
  167.     while (ffp->n_func != NULL) {
  168.         if (strcmp(fname, ffp->n_name) == 0)
  169.             return(ffp->n_func);
  170.         ++ffp;
  171.     }
  172.     return(NULL);
  173. }
  174.  
  175. /* bindtokey:    add a new key to the key binding table        */
  176.  
  177. bindtokey(f, n)
  178.  
  179. int f, n;    /* command arguments [IGNORED] */
  180.  
  181. {
  182.     register int c;        /* command key to bind */
  183.     register (*kfunc)();    /* ptr to the requexted function to bind to */
  184.     register char *ptr;    /* ptr to dump out input key string */
  185.     register KEYTAB *ktp;    /* pointer into the command table */
  186.     register int found;    /* matched command flag */
  187.     char outseq[80];    /* output buffer for keystroke sequence */
  188.     int (*getname())();
  189.  
  190.     /* prompt the user to type in a key to bind */
  191.     mlwrite(": bind-to-key ");
  192.  
  193.     /* get the function name to bind it to */
  194.     kfunc = getname();
  195.     if (kfunc == NULL) {
  196.         mlwrite("[No such function]");
  197.         return(FALSE);
  198.     }
  199.     TTputc(' ');        /* space it out */
  200.     TTflush();
  201.  
  202.     /* get the command sequence to bind */
  203.     c = getckey((kfunc == meta) || (kfunc == cex) ||
  204.                 (kfunc == unarg) || (kfunc == ctrlg));
  205.  
  206.     /* change it to something we can print as well */
  207.     cmdstr(c, &outseq[0]);
  208.  
  209.     /* and dump it out */
  210.     ptr = &outseq[0];
  211.     while (*ptr)
  212.         TTputc(*ptr++);
  213.  
  214.     /* if the function is a prefix key */
  215.     if (kfunc == meta || kfunc == cex ||
  216.         kfunc == unarg || kfunc == ctrlg) {
  217.  
  218.         /* search for an existing binding for the prefix key */
  219.         ktp = &keytab[0];
  220.         found = FALSE;
  221.         while (ktp->k_fp != NULL) {
  222.             if (ktp->k_fp == kfunc)
  223.                 unbindchar(ktp->k_code);
  224.             ++ktp;
  225.         }
  226.  
  227.         /* reset the appropriate global prefix variable */
  228.         if (kfunc == meta)
  229.             metac = c;
  230.         if (kfunc == cex)
  231.             ctlxc = c;
  232.         if (kfunc == unarg)
  233.             reptc = c;
  234.         if (kfunc == ctrlg)
  235.             abortc = c;
  236.     }
  237.  
  238.     /* search the table to see if it exists */
  239.     ktp = &keytab[0];
  240.     found = FALSE;
  241.     while (ktp->k_fp != NULL) {
  242.         if (ktp->k_code == c) {
  243.             found = TRUE;
  244.             break;
  245.         }
  246.         ++ktp;
  247.     }
  248.  
  249.     if (found) {    /* it exists, just change it then */
  250.         ktp->k_fp = kfunc;
  251.     } else {    /* otherwise we need to add it to the end */
  252.         /* if we run out of binding room, bitch */
  253.         if (ktp >= &keytab[NBINDS]) {
  254.             mlwrite("Binding table FULL!");
  255.             return(FALSE);
  256.         }
  257.  
  258.         ktp->k_code = c;    /* add keycode */
  259.         ktp->k_fp = kfunc;    /* and the function pointer */
  260.         ++ktp;            /* and make sure the next is null */
  261.         ktp->k_code = 0;
  262.         ktp->k_fp = NULL;
  263.     }
  264.     return(TRUE);
  265. }
  266.  
  267. /* unbindkey:    delete a key from the key binding table    */
  268.  
  269. unbindkey(f, n)
  270.  
  271. int f, n;    /* command arguments [IGNORED] */
  272.  
  273. {
  274.     register int c;        /* command key to unbind */
  275.     register char *ptr;    /* ptr to dump out input key string */
  276.     char outseq[80];    /* output buffer for keystroke sequence */
  277.  
  278.     /* prompt the user to type in a key to unbind */
  279.     mlwrite(": unbind-key ");
  280.  
  281.     /* get the command sequence to unbind */
  282.     c = getckey(FALSE);        /* get a command sequence */
  283.  
  284.     /* change it to something we can print as well */
  285.     cmdstr(c, &outseq[0]);
  286.  
  287.     /* and dump it out */
  288.     ptr = &outseq[0];
  289.     while (*ptr)
  290.         TTputc(*ptr++);
  291.  
  292.     /* if it isn't bound, bitch */
  293.     if (unbindchar(c) == FALSE) {
  294.         mlwrite("[Key not bound]");
  295.         return(FALSE);
  296.     }
  297.     return(TRUE);
  298. }
  299.  
  300. unbindchar(c)
  301.  
  302. int c;        /* command key to unbind */
  303.  
  304. {
  305.     register KEYTAB *ktp;    /* pointer into the command table */
  306.     register KEYTAB *sktp;    /* saved pointer into the command table */
  307.     register int found;    /* matched command flag */
  308.  
  309.     /* search the table to see if the key exists */
  310.     ktp = &keytab[0];
  311.     found = FALSE;
  312.     while (ktp->k_fp != NULL) {
  313.         if (ktp->k_code == c) {
  314.             found = TRUE;
  315.             break;
  316.         }
  317.         ++ktp;
  318.     }
  319.  
  320.     /* if it isn't bound, bitch */
  321.     if (!found)
  322.         return(FALSE);
  323.  
  324.     /* save the pointer and scan to the end of the table */
  325.     sktp = ktp;
  326.     while (ktp->k_fp != NULL)
  327.         ++ktp;
  328.     --ktp;        /* backup to the last legit entry */
  329.  
  330.     /* copy the last entry to the current one */
  331.     sktp->k_code = ktp->k_code;
  332.     sktp->k_fp   = ktp->k_fp;
  333.  
  334.     /* null out the last one */
  335.     ktp->k_code = 0;
  336.     ktp->k_fp = NULL;
  337.     return(TRUE);
  338. }
  339.  
  340. desbind(f, n)    /* describe bindings
  341.            bring up a fake buffer and list the key bindings
  342.            into it with view mode            */
  343.  
  344. #if    APROP
  345. {
  346.     buildlist(TRUE, "");
  347. }
  348.  
  349. apro(f, n)    /* Apropos (List functions that match a substring) */
  350.  
  351. {
  352.     char mstring[NSTRING];    /* string to match cmd names to */
  353.     int status;        /* status return */
  354.  
  355.     status = mlreply("Apropos string: ", mstring, NSTRING - 1);
  356.     if (status != TRUE)
  357.         return(status);
  358.  
  359.     return(buildlist(FALSE, mstring));
  360. }
  361.  
  362. buildlist(type, mstring)  /* build a binding list (limited or full) */
  363.  
  364. int type;    /* true = full list,   false = partial list */
  365. char *mstring;    /* match string if a partial list */
  366.  
  367. #endif
  368. {
  369.     register WINDOW *wp;    /* scnaning pointer to windows */
  370.     register KEYTAB *ktp;    /* pointer into the command table */
  371.     register NBIND *nptr;    /* pointer into the name binding table */
  372.     register BUFFER *bp;    /* buffer to put binding list into */
  373.     char *strp;        /* pointer int string to send */
  374.     int cpos;        /* current position to use in outseq */
  375.     char outseq[80];    /* output buffer for keystroke sequence */
  376.  
  377.     /* split the current window to make room for the binding list */
  378.     if (splitwind(FALSE, 1) == FALSE)
  379.             return(FALSE);
  380.  
  381.     /* and get a buffer for it */
  382.     bp = bfind("Binding list", TRUE, 0);
  383.     if (bp == NULL || bclear(bp) == FALSE) {
  384.         mlwrite("Can not display binding list");
  385.         return(FALSE);
  386.     }
  387.  
  388.     /* let us know this is in progress */
  389.     mlwrite("[Building binding list]");
  390.  
  391.     /* disconect the current buffer */
  392.         if (--curbp->b_nwnd == 0) {             /* Last use.            */
  393.                 curbp->b_dotp  = curwp->w_dotp;
  394.                 curbp->b_doto  = curwp->w_doto;
  395.                 curbp->b_markp = curwp->w_markp;
  396.                 curbp->b_marko = curwp->w_marko;
  397.         }
  398.  
  399.     /* connect the current window to this buffer */
  400.     curbp = bp;    /* make this buffer current in current window */
  401.     bp->b_mode = 0;        /* no modes active in binding list */
  402.     bp->b_nwnd++;        /* mark us as more in use */
  403.     wp = curwp;
  404.     wp->w_bufp = bp;
  405.     wp->w_linep = bp->b_linep;
  406.     wp->w_flag = WFHARD|WFFORCE;
  407.     wp->w_dotp = bp->b_dotp;
  408.     wp->w_doto = bp->b_doto;
  409.     wp->w_markp = NULL;
  410.     wp->w_marko = 0;
  411.  
  412.     /* build the contents of this window, inserting it line by line */
  413.     nptr = &names[0];
  414.     while (nptr->n_func != NULL) {
  415.  
  416.         /* add in the command name */
  417.         strcpy(outseq, nptr->n_name);
  418.         cpos = strlen(outseq);
  419.         
  420. #if    APROP
  421.         /* if we are executing an apropos command..... */
  422.         if (type == FALSE &&
  423.             /* and current string doesn't include the search string */
  424.             strinc(outseq, mstring) == FALSE)
  425.             goto fail;
  426. #endif
  427.         /* search down any keys bound to this */
  428.         ktp = &keytab[0];
  429.         while (ktp->k_fp != NULL) {
  430.             if (ktp->k_fp == nptr->n_func) {
  431.                 /* padd out some spaces */
  432.                 while (cpos < 25)
  433.                     outseq[cpos++] = ' ';
  434.  
  435.                 /* add in the command sequence */
  436.                 cmdstr(ktp->k_code, &outseq[cpos]);
  437.                 while (outseq[cpos] != 0)
  438.                     ++cpos;
  439.  
  440.                 /* and add it as a line into the buffer */
  441.                 strp = &outseq[0];
  442.                 while (*strp != 0)
  443.                     linsert(1, *strp++);
  444.                 lnewline();
  445.  
  446.                 cpos = 0;    /* and clear the line */
  447.             }
  448.             ++ktp;
  449.         }
  450.  
  451.         /* if no key was bound, we need to dump it anyway */
  452.         if (cpos > 0) {
  453.             outseq[cpos] = 0;
  454.             strp = &outseq[0];
  455.             while (*strp != 0)
  456.                 linsert(1, *strp++);
  457.             lnewline();
  458.         }
  459.  
  460. fail:        /* and on to the next name */
  461.         ++nptr;
  462.     }
  463.  
  464.     curwp->w_bufp->b_mode |= MDVIEW;/* put this buffer view mode */
  465.     curbp->b_flag &= ~BFCHG;    /* don't flag this as a change */
  466.     wp->w_dotp = lforw(bp->b_linep);/* back to the beginning */
  467.     wp->w_doto = 0;
  468.     wp = wheadp;            /* and update ALL mode lines */
  469.     while (wp != NULL) {
  470.         wp->w_flag |= WFMODE;
  471.         wp = wp->w_wndp;
  472.     }
  473.     mlwrite("");    /* clear the mode line */
  474.     return(TRUE);
  475. }
  476.  
  477. #if    APROP
  478. strinc(source, sub)    /* does source include sub? */
  479.  
  480. char *source;    /* string to search in */
  481. char *sub;    /* substring to look for */
  482.  
  483. {
  484.     char *sp;    /* ptr into source */
  485.     char *nxtsp;    /* next ptr into source */
  486.     char *tp;    /* ptr into substring */
  487.  
  488.     /* for each character in the source string */
  489.     sp = source;
  490.     while (*sp) {
  491.         tp = sub;
  492.         nxtsp = sp;
  493.  
  494.         /* is the substring here? */
  495.         while (*tp) {
  496.             if (*nxtsp++ != *tp)
  497.                 break;
  498.             else
  499.                 tp++;
  500.         }
  501.  
  502.         /* yes, return a success */
  503.         if (*tp == 0)
  504.             return(TRUE);
  505.  
  506.         /* no, onward */
  507.         sp++;
  508.     }
  509.     return(FALSE);
  510. }
  511. #endif
  512.  
  513. getckey(mflag)    /* get a command key sequence from the keyboard    */
  514.  
  515. int mflag;    /* going for a meta sequence? */
  516.  
  517. {
  518.     register int c;        /* character fetched */
  519.     register char *tp;    /* pointer into the token */
  520.     char tok[NSTRING];    /* command incoming */
  521.  
  522.     /* check to see if we are executing a command line */
  523.     if (clexec) {
  524.         macarg(tok);    /* get the next token */
  525.  
  526.         /* parse it up */
  527.         tp = &tok[0];
  528.         c = 0;
  529.  
  530.         /* first, the META prefix */
  531.         if (*tp == 'M' && *(tp+1) == '-') {
  532.             c = META;
  533.             tp += 2;
  534.         }
  535.  
  536.         /* next the function prefix */
  537.         if (*tp == 'F' && *(tp+1) == 'N') {
  538.             c |= SPEC;
  539.             tp += 2;
  540.         }
  541.  
  542.         /* control-x as well... */
  543.         if (*tp == '^' && *(tp+1) == 'X') {
  544.             c |= CTLX;
  545.             tp += 2;
  546.         }
  547.  
  548.         /* a control char? */
  549.         if (*tp == '^' && *(tp+1) != 0) {
  550.             c |= CTRL;
  551.             ++tp;
  552.         }
  553.  
  554.         /* make sure we are not lower case */
  555.         if (c >= 'a' && c <= 'z')
  556.             c -= 32;
  557.  
  558.         /* the final sequence... */
  559.         c |= *tp;
  560.  
  561.         return(c);
  562.     }
  563.  
  564.     /* or the normal way */
  565.     if (mflag)
  566.         c = get1key();
  567.     else
  568.         c = getcmd();
  569.     return(c);
  570. }
  571.  
  572. /* execute the startup file */
  573.  
  574. startup(sfname)
  575.  
  576. char *sfname;    /* name of startup file (null if default) */
  577.  
  578. {
  579.     char *fname;    /* resulting file name to execute */
  580.  
  581.     /* look up the startup file */
  582.     if (*sfname != 0)
  583.         fname = flook(sfname, TRUE);
  584.     else
  585.         fname = flook(pathname[0], TRUE);
  586.  
  587.     /* if it isn't around, don't sweat it */
  588.     if (fname == NULL)
  589.         return(TRUE);
  590.  
  591.     /* otherwise, execute the sucker */
  592.     return(dofile(fname));
  593. }
  594.  
  595. /*    Look up the existance of a file along the normal or PATH
  596.     environment variable. Look first in the HOME directory if
  597.     asked and possible
  598. */
  599.  
  600. char *flook(fname, hflag)
  601.  
  602. char *fname;    /* base file name to search for */
  603. int hflag;    /* Look in the HOME environment variable first? */
  604.  
  605. {
  606.     register char *home;    /* path to home directory */
  607.     register char *path;    /* environmental PATH variable */
  608.     register char *sp;    /* pointer into path spec */
  609.     register int i;        /* index */
  610.     register int status;    /* return status */
  611.     static char fspec[NSTRING];    /* full path spec to search */
  612.     char *getenv();
  613.  
  614. #if    ((MSDOS | AMIGA) & (LATTICE | AZTEC | MSC)) | V7 | USG | BSD
  615.  
  616.     if (hflag) {
  617.         home = getenv("HOME");
  618.         if (home != NULL) {
  619.             /* build home dir file spec */
  620.             strcpy(fspec, home);
  621.             strcat(fspec, "/");
  622.             strcat(fspec, fname);
  623.  
  624.             /* and try it out */
  625.             status = ffropen(fspec);
  626.             if (status == FIOSUC) {
  627.                 ffclose();
  628.                 return(fspec);
  629.             }
  630.         }
  631.     }
  632.  
  633.     /* get the PATH variable */
  634.     path = getenv("PATH");
  635.     if (path != NULL)
  636.         while (*path) {
  637.  
  638.             /* build next possible file spec */
  639.             sp = fspec;
  640.             while (*path && (*path != PATHCHR))
  641.                 *sp++ = *path++;
  642.             *sp++ = '/';
  643.             *sp = 0;
  644.             strcat(fspec, fname);
  645.  
  646.             /* and try it out */
  647.             status = ffropen(fspec);
  648.             if (status == FIOSUC) {
  649.                 ffclose();
  650.                 return(fspec);
  651.             }
  652.  
  653.             if (*path == PATHCHR)
  654.                 ++path;
  655.         }
  656. #endif
  657.  
  658.     /* look it up via the old table method */
  659.     for (i=2; i < NPNAMES; i++) {
  660.         strcpy(fspec, pathname[i]);
  661.         strcat(fspec, fname);
  662.  
  663.         /* and try it out */
  664.         status = ffropen(fspec);
  665.         if (status == FIOSUC) {
  666.             ffclose();
  667.             return(fspec);
  668.         }
  669.     }
  670.  
  671.     return(NULL);    /* no such luck */
  672. }
  673.  
  674.